package ciyuan.Structure;

import ciyuan.MainClass;
import net.minecraft.util.Mirror;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.ISeedReader;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.feature.*;
import net.minecraft.world.gen.feature.template.BlockIgnoreStructureProcessor;
import net.minecraft.world.gen.feature.template.PlacementSettings;
import net.minecraft.world.gen.feature.template.Template;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Random;

@Mod.EventBusSubscriber
public class FB1 {
    private static Feature<NoFeatureConfig> feature = null;
    private static ConfiguredFeature<?, ?> configuredFeature = null;

    public FB1() {
    }

    @SubscribeEvent
    public static void addFeatureToBiomes(BiomeLoadingEvent event) {
        boolean biomeCriteria = true;
        if ((new ResourceLocation("soul_sand_valley")).equals(event.getName())) {
            biomeCriteria = false;
        }

        if ((new ResourceLocation("warped_forest")).equals(event.getName())) {
            biomeCriteria = false;
        }

        if (biomeCriteria) {
            event.getGeneration().getFeatures(GenerationStage.Decoration.SURFACE_STRUCTURES).add(() -> {
                return configuredFeature;
            });
        }
    }

    @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
    private static class FeatureRegisterHandler {
        private FeatureRegisterHandler() {

        }

        @SubscribeEvent
        public static void registerFeature(RegistryEvent.Register<Feature<?>> event) {
            FB1.feature = new Feature<NoFeatureConfig>(NoFeatureConfig.CODEC) {
                @Override
                public boolean place(ISeedReader iSeedReader, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, NoFeatureConfig noFeatureConfig) {
                    int ci = blockPos.getX();
                    int ck = blockPos.getZ();
                    if (random.nextInt(1000)<100) {

                        int i = ci + random.nextInt(8);
                        int k = ck + random.nextInt(8);
                        int j = iSeedReader.getHeight(Heightmap.Type.WORLD_SURFACE_WG,i,k);
                        Rotation rotation = Rotation.values()[random.nextInt(3)];
                        Mirror mirror = Mirror.values()[random.nextInt(2)];
                        PlacementSettings placementSettings = new PlacementSettings().setRandom(random).addProcessor(BlockIgnoreStructureProcessor.STRUCTURE_AND_AIR);
                        BlockPos spawnTo = new BlockPos(i,j,k);
                        Template template = iSeedReader.getLevel().getStructureManager().getOrCreate(new ResourceLocation(MainClass.MODID,"fbin2"));
                        Template template2 = iSeedReader.getLevel().getStructureManager().getOrCreate(new ResourceLocation(MainClass.MODID,"fbin2"));
                        System.out.println("生成: " + i + " " + j + " " + k);
                        BlockPos spawnTo2 = new BlockPos(i + 19,j,k);
                        template.placeInWorld(iSeedReader,spawnTo,placementSettings,random);
                        template2.placeInWorld(iSeedReader,spawnTo2,placementSettings,random);

                        System.out.println("生成完成");
                    }

                    return true;
                }
            };
            FB1.configuredFeature = FB1.feature.configured(IFeatureConfig.NONE).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE).chance(32);
            event.getRegistry().register((Feature) FB1.feature.setRegistryName("fbin2"));
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"fbin2", FB1.configuredFeature);
        }
    }
}
